import { fileURLToPath, URL } from "node:url";
import Components from "unplugin-vue-components/vite";
import AutoImport from "unplugin-auto-import/vite";
import { defineConfig } from "vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import vue from "@vitejs/plugin-vue";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
      // 解析的文件目录
      dirs: ["src/components"],
      // 组件名称 包含目录 ，防止命名冲突
      directoryAsNamespace: true,
    }),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  // 配置前端服务地址和端口
  server: {
    port: 5005, // 启动端口
    open: false, // 启动后是否打开浏览器。建议关闭，首次打开的页面会热更新，否则每次都会打开新的tab页。
    cors: true, // 允许跨域
    https: false,
    proxy: {
      "/api": {
        target: "http://codercba.com:8000",
        changeOrigin: true, //是否改变源
        rewrite: (path) => path.replace(/^\/api/, ""), //路径重写
      },
    },
  },
  //配置跨域代码结束
});
